package libaryinterface.login;

import libaryinterface.interfaceutils.PrivateInterface;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
// 注册界面
public class RegistrationInterface {
    JFrame f2,jf;
    String pefession[];
    JPanel p2;
    JButton b2,b1;
    JLabel l3, l4, l5,l6,l7;
    JTextField t3,t4;
    JPasswordField s2, s3;
    JComboBox c1,c2,c3;//下拉选择框
    public RegistrationInterface() {
        b1 = new JButton("返回初始界面");
        String[] pefession = {"本科生","研究生","教员职工"};
        f2 = new JFrame("注册");
        p2 = new JPanel();
        b2 = new JButton("确认");
        l3 = new JLabel("学号：");
        t3 = new JTextField(10);
        t4 = new JTextField(10);
        c1 = new JComboBox(pefession);
        l4 = new JLabel("密码：");
        l5 = new JLabel("确认密码：");
        s2 = new JPasswordField(18);
        s3 = new JPasswordField(18);
        l6 =new JLabel("昵称：");
        l7 =new JLabel("人员类别：");
        ImageIcon bg = new ImageIcon("src//picture//regist.png");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        f2.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        p2=(JPanel)f2.getContentPane();
        p2.setOpaque(false);
        p2.setLayout((new FlowLayout()));
        p2.setLayout(null);
        b1.setFont(new Font("黑体",Font.PLAIN,10));
        b1.setBounds(50,500,100,30);
        l6.setBounds(120,100,39,20);//昵称
        t3.setBounds(160, 100, 150, 20);
        l3.setBounds(120, 150, 40, 20);//学号
        t4.setBounds(160, 150, 150, 20);
        b2.setBounds(160, 360, 100, 50);
        l4.setBounds(120, 200, 40, 20);//密码1
        s2.setBounds(160, 200, 150, 20);
        l5.setBounds(95,250,80,20);//密码2
        s3.setBounds(160,250,150,20);
        c1.setBounds(160, 300, 150, 20);//人员类别
        l7.setBounds(94,300,70,20);
        b2.setBackground(Color.orange);
        l3.setForeground(Color.white);
        l4.setForeground(Color.white);
        l5.setForeground(Color.white);
        l6.setForeground(Color.white);
        l7.setForeground(Color.white);
        p2.add(b1);
        p2.add(l3);
        p2.add(l4);
        p2.add(l5);
        p2.add(s2);
        p2.add(s3);
        p2.add(b2);
        p2.add(l6);
        p2.add(l7);
        p2.add(t3);
        p2.add(t4);
        p2.add(c1);
        f2.setVisible(true);
        f2.setBounds(600, 250, 460, 600);

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = t4.getText();//学号
                String password = new String(s2.getPassword());//密码
                String CheckPassWord = new String(s3.getPassword());//确认的密码
                String nick_name = t3.getText();//昵称
                long catelogy_id; //人员类别
                if(id.equals("") || password.equals("") || CheckPassWord.equals("") || nick_name.equals("")){
                    JOptionPane.showMessageDialog(null,  "输入的信息不全","错误", JOptionPane.ERROR_MESSAGE);
                    return ;
                }
                if(c1.getSelectedItem().toString().equals("本科生"))
                {
                    catelogy_id=1;
                }
                else if(c1.getSelectedItem().toString().equals("研究生"))
                {
                    catelogy_id=2;
                }
                else catelogy_id=3;
                UserService userService=new UserServiceImpl();
                if(!password.equals(CheckPassWord))
                {
                    JOptionPane.showMessageDialog(null, "密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                }
                else {

                    User number = userService.Search(id);
                    if (number!=null) {
                        JOptionPane.showMessageDialog(null, "错误", "账户已注册", JOptionPane.ERROR_MESSAGE);
                    }
                    else {
                        userService.register(id, password, nick_name, catelogy_id);
                        JOptionPane.showMessageDialog(null, "注册成功", "", JOptionPane.OK_CANCEL_OPTION);
                        f2.dispose();
                        new PrivateInterface();
                    }
                }

            }
        } );
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new PrivateInterface();
                f2.dispose();
            }
        });
        f2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}
